<template>
  <div>
    <template v-if="showHeader">
      <v-header :seller="seller"></v-header>
      <div class="tab border-1px">
        <div class="tab-item">
          <router-link to="/goods">商品</router-link>
        </div>
        <div class="tab-item">
          <!-- <router-link to="/ratings">评论</router-link> -->
        </div>
        <div class="tab-item">
          <router-link to="/myindex">我的</router-link>
        </div>
      </div>
    </template>
      <keep-alive>
        <router-view :seller="seller" :showHeader="showHeader"></router-view>
      </keep-alive>
  </div>
</template>

<script type="text/ecmascript-6">
  import { urlParse } from 'common/js/util';
  import header from 'components/header/header.vue';
  // import { startWebSocket } from './utils/websocket.js';
  const ERR_OK = 0;
  const debug = process.env.NODE_ENV !== 'production';

  export default {
    data() {
      return {
        seller: {
          id: (() => {
            let queryParam = urlParse();
            return queryParam.id;
          })()
        },
        showHeader: true
      };
    },
    methods: {
      changeHash() {
        const hash = window.location.hash;
        console.log(hash);
        if (hash.indexOf('payment') > -1 || hash.indexOf('order') > -1 || hash.indexOf('placeorder') > -1) {
          this.showHeader = false;
        } else {
          this.showHeader = true;
        }
      }
    },
    mounted() {
      // console.log('startWebSocket');
      // 发起ws请求, cqzwsf.nat300.top 这个域名有问题，经常连接不上 127.0.0.1:5001
      // const wsuri = 'ws://cqzwsf.nat300.top/apisell/webSocketBuyer';
      // const wsuri = 'ws://127.0.0.1:5001/apisell/webSocketBuyer';
      // startWebSocket(wsuri);
    },
    created() {
      this.changeHash();
      window.addEventListener('hashchange', () => {
        this.changeHash();
      });
      const url = debug ? '/seller' : '/seller';
      this.$axios.get(url + '?id=' + this.seller.id).then((response) => {
        response = response.data;
        if (response.code === ERR_OK) {
          this.seller = Object.assign({}, this.seller, response.data);
        }
      });
    },
    components: {
      'v-header': header
    }
  };
</script>

<style lang="stylus" rel="stylesheet/stylus">
  @import "./common/stylus/mixin.styl"

  .tab
    display: flex
    width: 100%
    height: 40px
    line-height: 40px
    border-1px(rgba(7, 17, 27, 0.1))
    .tab-item
      flex: 1
      text-align: center
      & > a
        display: block
        font-size: 14px
        color: rgb(77, 85, 93)
        &.active
          color: rgb(240, 20, 20)
</style>
